package com.foxmail.yusp75.myexam;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;


public class MultipleFragment extends Fragment {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String c_answer;

    private CheckBox cb_a, cb_b, cb_c, cb_d;

    private OnFragmentInteractionListener mListener;

    public MultipleFragment() {
    }

    public static MultipleFragment newInstance(String param1, String param2) {
        MultipleFragment fragment = new MultipleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_multiple, container, false);
        cb_a = view.findViewById(R.id.multiple_a);
        cb_a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (mListener != null) {
                    make_answer();
                    mListener.onFragmentInteraction(c_answer);
                }
            }
        });
        cb_b = view.findViewById(R.id.multiple_b);
        cb_b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (mListener != null) {
                    make_answer();
                    mListener.onFragmentInteraction(c_answer);
                }
            }
        });

        cb_c = view.findViewById(R.id.multiple_c);
        cb_c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (mListener != null) {
                    make_answer();
                    mListener.onFragmentInteraction(c_answer);
                }
            }
        });

        cb_d = view.findViewById(R.id.multiple_d);
        cb_d.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (mListener != null) {
                    make_answer();
                    mListener.onFragmentInteraction(c_answer);
                }
            }
        });

        return view;
    }

    public void setmListener(OnFragmentInteractionListener listener) {
        mListener = listener;
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    private void make_answer() {
        String a, b, c, d;
        if (cb_a.isChecked())
            a = cb_a.getText().toString();
        else
            a = "";
        if (cb_b.isChecked())
            b = cb_b.getText().toString();
        else
            b = "";
        if (cb_c.isChecked())
            c = cb_c.getText().toString();
        else
            c = "";
        if (cb_d.isChecked())
            d = cb_d.getText().toString();
        else
            d = "";

        c_answer = String.format("%s%s%s%s", a, b, c, d);
    }

    public void reset() {
        if (cb_a != null)
            cb_a.setChecked(false);
        if (cb_b != null)
            cb_b.setChecked(false);
        if (cb_c != null)
            cb_c.setChecked(false);
        if (cb_d != null)
            cb_d.setChecked(false);
    }

    // interface
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String c);
    }
}
